/*
 * Copyright (c) 2010, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the [QTPRJ] nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL [QTPRJ] BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package joodin.impl.widgets;

import java.util.ArrayList;
import java.util.List;

import joodin.impl.widgets.internal.ActionMenuItemImpl;
import joodin.impl.widgets.internal.SelectableMenuItemImpl;
import joodin.impl.widgets.internal.SubMenuImpl;

import org.jowidgets.common.widgets.controler.IMenuListener;
import org.jowidgets.spi.impl.controler.MenuObservable;
import org.jowidgets.spi.widgets.IActionMenuItemSpi;
import org.jowidgets.spi.widgets.IMenuItemSpi;
import org.jowidgets.spi.widgets.IMenuSpi;
import org.jowidgets.spi.widgets.ISelectableMenuItemSpi;
import org.jowidgets.spi.widgets.ISubMenuSpi;

import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.MenuItem;

public class VaadinMenu implements IMenuSpi {

	private final MenuItem menu;

	private final MenuObservable menuObservable;

//	private QAction lastAction;
//	private QTimer timer;

	public VaadinMenu(final MenuItem menu) {
		this.menu = menu;
		this.menuObservable = new MenuObservable();

//		menu.hovered.connect(this, "hover(QAction)");
//		menu.aboutToHide.connect(this, "menuHide()");
//		menu.aboutToShow.connect(this, "menuShow()");
	}

	protected void menuHide() {
//		QToolTip.hideText();
//		lastAction = null;
//		menuObservable.fireMenuDeactivated();
	}

	protected void menuShow() {
		menuObservable.fireMenuActivated();
	}

	@Override
	public MenuItem getUiReference() {
		return menu;
	}

	@Override
	public void remove(final int index) {
		menu.getChildren().remove(index);
	}

	@Override
	public void setEnabled(final boolean enabled) {
		menu.setEnabled(enabled);
	}

	@Override
	public boolean isEnabled() {
		return menu.isEnabled();
	}

	private ActionMenuItemImpl createActionItem(final MenuItem menuItem) {
		final ActionMenuItemImpl result = new ActionMenuItemImpl(menuItem);
		return result;
	}

	protected List<MenuItem> getActionGroup(final MenuItem action) {
		final List<MenuItem> actions = menu.getChildren();
		final int pos = actions.indexOf(action);
		final List<MenuItem> actionGroup = new ArrayList<MenuBar.MenuItem>();

		int index = pos - 1;
		while (index >= 0) {
			final MenuItem current = actions.get(index);
			if (current.isSeparator()) {
				break;
			}

			actionGroup.add(current);

			index--;
		}

		index = pos + 1;
		while (index < actions.size()) {
			final MenuItem current = actions.get(index);
			if (current.isSeparator()) {
				break;
			}

			actionGroup.add(current);

			index++;
		}

		return actionGroup;
	}

	@Override
	public IActionMenuItemSpi addActionItem(final Integer index) {
		
		MenuItem item = menu.addItem("", null);;
		if(index != null){
			menu.getChildren().set(index, item);
		}
	
		return createActionItem(item);
	}

	@Override
	public ISelectableMenuItemSpi addCheckedItem(final Integer index) {
		
		MenuItem item = menu.addItem("", null);;
		if(index != null){
			menu.getChildren().set(index, item);
		}
	
		return createSelectableItem(item);
	}

	@Override
	public ISelectableMenuItemSpi addRadioItem(final Integer index) {
		MenuItem item = menu.addItem("", null);;
		if(index != null){
			menu.getChildren().set(index, item);
		}
		return createSelectableItem(item);
	}

	@Override
	public IMenuItemSpi addSeparator(final Integer index) {
		MenuItem separator = menu.addSeparator();
		if(index != null){
			separator = menu.getChildren().set(index, separator);
		}
		
		return createActionItem(separator);
	}

	private SelectableMenuItemImpl createSelectableItem(final MenuItem menuItem) {
		final SelectableMenuItemImpl result = new SelectableMenuItemImpl(menuItem);
		return result;
	}

	@Override
	public ISubMenuSpi addSubMenu(final Integer index) {
		MenuItem item = menu.addItem("", null);;
		if(index != null){
			menu.getChildren().set(index, item);
		}
		return createSubMenu(item);
	}

	private SubMenuImpl createSubMenu(final MenuItem subMenuItem) {
		final SubMenuImpl result = new SubMenuImpl(subMenuItem);
		return result;
	}

	@Override
	public void addMenuListener(final IMenuListener listener) {
		menuObservable.addMenuListener(listener);
	}

	@Override
	public void removeMenuListener(final IMenuListener listener) {
		menuObservable.removeMenuListener(listener);
	}
}
